const heroesNames = `hooded-assassin.png, king.png, mounted-knight.png,ninja-heroic-stance.png, robe.png, robot-golem.png,spy.png, swordman.png, swordwoman`.replace(/, ?/g, "").split('.png');
const heroesTips = [
	["刺客", "一个身手敏捷的刺客"],
	["国王", "一个威严的国王"],
	["骑士", "一个正直勇敢的骑士"],
	["忍者", "一个狡猾善变的忍者"],
	["法师", "一个充满智慧的法师"],
	["机器人", "一个来自未来的机器人"],
	["侦探", "一个敏锐的侦探"],
	["勇者♂", "一个男性勇士"],
	["勇者♀", "一个女性勇士"],
]
// 英雄影响技能池和势力池
class Hero {
	constructor(i) {
		// 名称
		this.name = heroesTips[i][0]
		// 职业编号
		this.type = i;
		// 存放已拥有的物品的id
		this.itemId = [];
		// 物品相关的缓存
		this.itemInfo = {
			treasureHouse: [],
			monsterReward: []
		};
		this.heroInfo = {
			// 等级
			level: 0,
			// 属性: 血 蓝 攻 防 命 运 速 暴 
			abilities: [60, 30, 28, 25, 15, 8, 8, 8],
			// 经验
			exp: [0, 65],
			// 英雄成长率
			growRate: [8, 4, 3, 3, 2, 1, 1, 1],
			// 已拥有的技能 ID 11,12,3,4
			hasMove: [],
			// 当前上阵的技能 false,false,false,false 表示全部都禁用
			activeMoveFlag: [],
			// 饱食度
			// repletion: 100,
			// 移动力
			// movementPoint: 8,
			// 探索范围
			// viewDistance: 1,
			// 额外奖励
			// extraReward: 0,
		}
		this.petInfo = {
			// 捕捉等级
			catchLevel: 0,
			// 上阵数量 备战数量
			petNum: [1, 1],
			// 额外捕捉几率
			extraCatchRate: 0,
			// 出战和休息的宠物信息 里面放 {stat:object ,mid:number }
			battlePet: [],
			readyPet: [],
			// 上一次死亡的宠物 mid
			lastDeadPet: null
		}
		const initHero = (i) => {
			let vals = [], rates = [];
			switch (i) {
				case 0:
					vals = [0, -15, 5, -10, 5, 5, 5, 5];
					rates = [1, 1, 2, 2, 1, 2, 1, 1];
					this.faction = [0, 1, 2, 3];
					this.moveTree = [0, 1, 2, 3]
					break;
				case 1:
					vals = [10, 10, -5, -5, 4, 4, 3, 4];
					rates = [2, 2, 1, 1, 2, 1, 1, 1]
					this.faction = [4, 5, 6, 7];
					this.moveTree = [4, 5, 6, 7]
					break;
				case 7:
					vals = [10, 0, 5, 2, 4, 2, 1, 1];
					rates = [2, 2, 1, 1, 2, 1, 1, 1]
					this.faction = [8, 9, 10, 11];
					this.moveTree = [8, 9, 10, 11]
					break;
				case 8:
					vals = [10, 3, 2, 2, 2, 4, 1, 1];
					rates = [2, 2, 1, 1, 1, 2, 1, 1]
					this.faction = [11, 8, 4, 2];
					this.moveTree = [15, 14, 12, 13]
					break;
				default:
					break;
			}
			for (let i = 0; i < vals.length; i++) {
				this.heroInfo.abilities[i] += vals[i];
				this.heroInfo.growRate[i] += rates[i];
			}
			this.heroInfo.hp = this.heroInfo.abilities[0]
			this.heroInfo.mp = this.heroInfo.abilities[1]
		}
		initHero(i);
	}
	learnMove() {
		if (this.heroInfo.exp[0] > 50) {
			this.heroInfo.exp[0] - 50;
			game.selectedMove = getRandomRange(0, 23);
			this.heroInfo.hasMove.push(game.selectedMove);
			this.heroInfo.activeMoveFlag.push(true);
			renderHeroMove(this.heroInfo.hasMove.length - 1)
		}
	}
	getExp(_exp) {
		this.heroInfo.exp[0] += _exp;
		if (this.heroInfo.exp[0] > this.heroInfo.exp[1]) {
			this.levelUp()
			renderTips('升级了')
		} else {
			// 更新视图
			renderHeroBar();
		}
	}
	levelUp() {
		++this.heroInfo.level;
		for (let i = 0; i < 8; i++) {
			this.heroInfo.abilities[i] += this.heroInfo.level * (this.heroInfo.growRate[i] + 1);
		}
		this.heroInfo.hp = this.heroInfo.abilities[0];
		this.heroInfo.mp = this.heroInfo.abilities[1];
		this.heroInfo.exp[0] -= this.heroInfo.exp[1];
		this.heroInfo.exp[1] += 10 * this.heroInfo.level + 80;
		// 更新视图
		renderHeroBar();
	}
	catchPet(mid) {
		// 计算成功率
		if (getRandomRange(1, 100) < 60) {
			this.getPet(mid)
			renderTips('捕捉成功')
			return true
		} else {
			renderTips('捕捉失败')
			return false
		}
	}
	getPet(mid) {
		let _place = null;
		// 当前只能出战一只，休息一只
		if (this.petInfo.battlePet.length == 0) {
			_place = this.petInfo.battlePet
		} else if (this.petInfo.readyPet.length == 0) {
			_place = this.petInfo.readyPet
		}
		if (_place) {
			if (mid<0||mid>42||!mid) {
				console.log("没有这样的怪兽");
			}
			let _p = {
				mid,
				stat: []
			}
			for (let i = 0; i < 8; i++) {
				_p.stat[i] = Math.round(monsterData[mid].a[i] * monsterData[mid].b[i]);
			}
			_place.push(_p);
			renderTips('你获得宠物')
			// 获得宠物
			console.log("获得宠物", _place);
			// 选中了 mid 0 的宠物
			game.selectedPet = mid;
			// 新获得的宠物渲染出来
			renderHeroPet(this.petInfo.readyPet.length + this.petInfo.battlePet.length - 1);
		}
	}
	getItem(id) {
		if (this.itemId.length < 12) {
			id = Number(id)
			// 首次加入数组后，数组长度变为1
			this.itemId.push(id)
			// 渲染索引0的物品
			renderHeroItem(this.itemId.length - 1)
			if (typeof itemData[id].get == 'function') {
				itemData[id].get()
				// 更新血条蓝条
				renderHeroBar();
			}
			if (typeof itemData[id].once == 'function') {
				eventQueue.push(itemData[id].once)
			}
			renderTips('获得物品')
		}
	}
	lostPetInBattle(i) {
		if (typeof this.petInfo.battlePet[i] == 'object') {
			this.petInfo.lastDeadPet = this.petInfo.battlePet[i].mid;
			// 更新视图
			this.petInfo.battlePet.splice(i, 1);
			renderHeroPetDead(i)
			renderTips('出战宠物死亡')
		}
	}
}